/**
 * User: alesj
 * Date: 2004.8.23
 * Time: 14:56:53
 */

package com.generalynx.common.web.module;

import com.generalynx.common.data.IEntity;
import com.generalynx.common.resources.Resources;
import com.generalynx.common.web.tags.SelectOption;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.Ordered;

import java.util.Set;
import java.io.Serializable;

/**
 * @hibernate.class table="modul" mutable="false"
 * @hibernate.cache usage="read-only"
 */
public class Module implements Ordered, IEntity, SelectOption {

    protected final Log logger = LogFactory.getLog(getClass());
    public static final int DYNAMIC_MODULE_ID_BEGIN = 10000;

    private int m_id;
    private String m_name;
    private int m_status;
    private Module m_parent;
    private Set m_children;
    private String m_action;
    private String m_query;
    private String m_description;
    private int m_order;
    private boolean m_showHelp;
    private Set m_users;

    public boolean equals(Object obj) {
        return getId() == ((Module)obj).getId();
    }

    /**
     * Since we only add modules through database,
     * therefore id can never be 0 or duplicated.
     */
    public int hashCode() {
        return getId();
    }

    public String toString() {
        return getName();
    }

    public String getLabel() {
        try {
            return Resources.getString("MODULES_" + getName());
        } catch (Exception e) {
            return getName();
        }
    }

    public boolean isAssociation() {
        return true;
    }

    public Serializable getIdentifier() {
        return new Integer(getId());
    }

    public boolean isCacheable() {
        return false;
    }

    /**
     * @hibernate.id  generator-class="native" unsaved-value="0" column="modul_id"
     */
    public int getId() {
        return m_id;
    }

    public void setId(int id) {
        m_id = id;
    }

    /**
     * @hibernate.property
     */
    public String getName() {
        return m_name;
    }

    public void setName(String name) {
        m_name = name;
    }

    /**
     * @hibernate.property
     */
    public int getStatus() {
        return m_status;
    }

    public void setStatus(int status) {
        m_status = status;
    }

    /**
     * @hibernate.many-to-one column="parent_id"
     */
    public Module getParent() {
        return m_parent;
    }

    public void setParent(Module parent) {
        m_parent = parent;
    }

    /**
     * @hibernate.set inverse="true" table="modul" lazy="false" cascade="all" where="status > 0" order-by="orderby"
     * @hibernate.cache usage="read-write"
     * @hibernate.collection-key column="parent_id"
     * @hibernate.collection-one-to-many  class="com.generalynx.common.web.module.Module"
     */
    public Set getChildren() {
        return m_children;
    }

    public void setChildren(Set children) {
        m_children = children;
    }

    /**
     * @hibernate.property
     */
    public String getDescription() {
        return m_description;
    }

    public void setDescription(String description) {
        m_description = description;
    }

    /**
     * @hibernate.property column="orderby"
     */
    public int getOrder() {
        return m_order;
    }

    public void setOrder(int order) {
        m_order = order;
    }

    /**
     * @hibernate.property column="maction"
     */
    public String getAction() {
        return m_action;
    }

    public void setAction(String action) {
        m_action = action;
    }

    /**
     * @hibernate.property column="mquery"
     */
    public String getQuery() {
        return m_query;
    }

    public void setQuery(String query) {
        m_query = query;
    }

    /**
     * @hibernate.property
     */
    public boolean getShowHelp() {
        return m_showHelp;
    }

    public void setShowHelp(boolean showHelp) {
        m_showHelp = showHelp;
    }

    /**
     * @hibernate.set name="modules" inverse="true" table="moduleuser" lazy="true" cascade="none"
     * @hibernate.collection-key column="modul_id"
     * @hibernate.collection-many-to-many column="modul_id" class="com.generalynx.ecos.data.User"
     */
    public Set getUsers() {
        return m_users;
    }

    public void setUsers(Set users) {
        m_users = users;
    }

}
